<?php
session_start();
include_once 'db.php';
if(!$_SESSION["passed"]) return false;


$actionType = $_POST["atype"];
$param = $_POST["param"];
// $param = json_decode($queryParam, true);

function get_user_num($query_where, $query_param) {
    $query_str = "select count(*) as count from l_user where 1 = 1 ".$query_where;
    // echo $query_str;
    $count = mysql_fetch_assoc(dbOperate($query_str, $query_param));
    $count = $count['count'];
    return $count ? $count : 0;
};

function insert_user($param) {
    $result = 0;
    $user_type = $param['user_type'];
    $password = $param['password'];
    $user_name = $param['user_name'];
    $nick_name = urldecode($param['nick_name']);
    $real_name = urldecode($param['real_name']);
    $gender = $param['gender'];
    $address = urldecode($param['address']);
    $email = urldecode($param['email']);
    $phone = $param['phone'];
    $desc = urldecode($param['desc']);
    if(is_null($user_type) || !$password || !$user_name) return $result;

    $query_str = "insert into l_user (id, user_code, user_type, password, user_name, nick_name, real_name, gender, address,"
        ." email, phone, description, last_modify) values ('', '".uniqid()."', %i, %q, %q, %q, %q, %i, %q, %q, %q, %q, now())";
    $query_param = array($user_type, $password, $user_name, $nick_name, $real_name, $gender, $address, $email, $phone, $desc);

    $result = dbOperate($query_str, $query_param);
    return $result;
};

function delete_user($param) {
    $result = 0;
    $m_code = $param['user_code'];
    if(!$user_code) return $result;
    $query_str = "delete from l_user where user_code = %q";
    $query_param = array($user_code);

    $result = dbOperate($query_str, $query_param);
    return $result;
};

function update_user($param) {
    $result = 0;
    $user_code = $param['user_code'];
    $user_type = $param['user_type'];
    $password = $param['password'];
    $user_name = $param['user_name'];
    $nick_name = urldecode($param['nick_name']);
    $real_name = urldecode($param['real_name']);
    $gender = $param['gender'];
    $address = urldecode($param['address']);
    $email = urldecode($param['email']);
    $phone = $param['phone'];
    $desc = urldecode($param['desc']);
    // echo urldecode($param['desc']);
    $clip = array();
    $query_param = array();

    if($user_type) {
        array_push($clip, 'user_type = %i, ');
        array_push($query_param, $user_type);
    }
    if($password) {
        array_push($clip, 'password = %q, ');
        array_push($query_param, $password);
    }
    if($user_name) {
        array_push($clip, 'user_name = %q, ');
        array_push($query_param, $user_name);
    }
    if($nick_name) {
        array_push($clip, 'nick_name = %q, ');
        array_push($query_param, $nick_name);
    }
    if($real_name) {
        array_push($clip, 'real_name = %q, ');
        array_push($query_param, $real_name);
    }
    if($gender) {
        array_push($clip, 'gender = %i, ');
        array_push($query_param, $gender);
    }
    if($address) {
        array_push($clip, 'address = %q, ');
        array_push($query_param, $address);
    }
    if($email) {
        array_push($clip, 'email = %q, ');
        array_push($query_param, $email);
    }
    if($phone) {
        array_push($clip, 'phone = %q, ');
        array_push($query_param, $phone);
    }
    if($desc) {
        array_push($clip, 'description = %q, ');
        array_push($query_param, $desc);
    }
    array_push($query_param, $user_code);
    $query_str = "update l_user set ".join('', $clip)."last_modify = now() where user_code = %q";

    $result = dbOperate($query_str, $query_param);
    return $result;
};

function query_user($param) {
    $result = 0;

    // var_dump($param);
    // echo $param['user_code'].",".$param['user_name']."  ";
    $user_type = $param['userType'];
    $user_code = $param['userCode'];
    $user_name = $param['userName'];
    $page = $param['page'] ? $param['page'] : 1;
    $page_size = $param['pageSize'] ? $param['pageSize'] : 30;
    $sortName = $param['sortName'] ? $param['sortName'] : 'last_modify';
    $sortOrder = $param['sortOrder'] ? $param['sortOrder'] : 'desc';

    $query_param = array();
    $clip = "";
    // if(!$user_code || !$user_name) return $result;
    if($user_code) {
        $clip = $clip." and user_code = %q ";
        array_push($query_param, $user_code);
    }
    if($user_type) {
        $clip = $clip." and user_type = %i ";
        array_push($query_param, $user_type);
    }

    if($user_name) {
        $clip = $clip." and instr('user_name', '%s') > -1 ";
        array_push($query_param, $user_name);
    }
    // echo $user_code.",".$user_name."  ";

    $count = get_user_num($clip, $query_param);
    $pages = intval($count / $page_size);
    $pages = $count % $page_size > 0 ? $pages + 1 : $pages;

    array_push($query_param, $page - 1);
    array_push($query_param, $page_size);
    // var_dump($query_param);
    $query_str = "select l.*, (select g_desc from d_gender where l.gender = g_code) as status from l_user l where 1 = 1 ".$clip." order by ".$sortName." ".$sortOrder." limit %i, %i";
    // echo $query_str;
    $result = dbOperate($query_str, $query_param);

    $arr = array();
    while($row = mysql_fetch_assoc($result))
    {
      array_push($arr, $row);
    }
    $rows = array("Rows"=>$arr, "curPage"=>$page, "pages"=>$pages);

    return json_encode($rows);
}

$result = 0;
switch($actionType) {
    case "c":
        $result = insert_user($param);
        break;
    case "r":
        $result = query_user($param);
        break;
    case "u":
        $result = update_user($param);
        break;
    case "d":
        $result = delete_user($param);
        break;
    default:
        $result = 0;
        break;
}
echo $result;

?>